import unittest
from unittest.mock import MagicMock

import bs4

from buondua.scrappers.album import AlbumParser


class TestAlbumParser(unittest.TestCase):

    def setUp(self):
        def get_document():
            with open('tests/fixtures/hot_albums.html', 'rb') as f:
                return bs4.BeautifulSoup(f.read(), 'html.parser')

        self.album_parser = AlbumParser()
        self.album_parser.get_document = MagicMock(return_value=get_document())
        self.album_parser.document = self.album_parser.get_document()

    def test_extraction_data(self):
        self.assertEqual(
            self.album_parser.extract_albums(),
            [
                b'https://buondua.com/pure-media-vol-270-hina-\xed\x9e\x88\xeb\x82\x98-sira-\xec\x8b\x9c\xeb\x9d\xbc-\xe2\x80\x93-x-rated-contract-248-photos-35178',
                b'https://buondua.com/pure-media-vol-269-yeha-\xec\x98\x88\xed\x95\x98-\xe2\x80\x93-visit-suspicious-hospital-201-photos-35108',
                b'https://buondua.com/djawa-photo-\xe2\x80\x93-hendoong-\xe2\x80\x9cswimming-lessons-14\xe2\x80\x9d-147-photos-35262',
                b'https://buondua.com/le-lehf-095a-b-c-g-su-165-photos-35214',
                b'https://buondua.com/pure-media-vol-271-min-ah-\xeb\xaf\xbc\xec\x95\x84-\xe2\x80\x93-kidnapping-119-photos-35236',
                b'https://buondua.com/coser-\xe6\xb0\xb4\xe6\xb7\xbcaqua-\xe7\xa2\xa7\xe8\x93\x9d\xe6\xa1\xa3\xe6\xa1\x88-\xe2\x80\x93-\xe4\xb8\x80\xe4\xb9\x8b\xe6\xbf\x91\xe6\x98\x8e\xe6\x97\xa5\xe5\xa5\x88\xe5\xa5\xb3\xe4\xbb\x86b-155-photos-35231',
                b'https://buondua.com/fantasy-story-white-sugar-vol-1-dress-check-for-a-poopy-skirt-113-photos-35285',
                b'https://buondua.com/bimilstory-hayun-\xed\x95\x98\xec\x9c\xa4-vol-01-debut-\xe2\x80\x93-flower-in-blossom-part-1-91-photos-35270',
                b'https://buondua.com/bimilstory-bomi-\xeb\xb3\xb4\xeb\xaf\xb8-vol-40-oil-shower-100-photos-35192',
                b'https://buondua.com/kang-in-kyung-\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos-35006',
                b'https://buondua.com/fantasy-story-banhee-vol-6-cafe-female-owner-98-photos-35218',
                b'https://buondua.com/glamarchive-baek-sul-tang-vol-19-football-fan-47-photos-35200',
                b'https://buondua.com/glamarchive-woo-u-\xec\x9a\xb0\xec\x9c\xa0-vol-13-diva-40-photos-35237',
                b'https://buondua.com/pure-media-vol-267-hina-\xed\x9e\x88\xeb\x82\x98-\xe2\x80\x93-black-out-slutty-girl-244-photos-34769',
                b'https://buondua.com/le-ledg-064a-jeong-ah-\xec\xa0\x95\xec\x95\x84-50-photos-35234',
                b'https://buondua.com/maruemon-\xeb\xa7\x88\xeb\xa3\xa8\xec\x97\x90\xeb\xaa\xbd-bunny-87-photos-35158',
                b'https://buondua.com/rina\xe3\x83\xa2\xe3\x83\xa2\xe3\x83\xaa\xe3\x83\x8a-momorina-momo-tako-127-photos-35161',
                b'https://buondua.com/bimilstory-nara-\xeb\x82\x98\xeb\x9d\xbc-vol-33-fantasy-harness-112-photos-35217',
                b'https://buondua.com/coser-\xe4\xb9\x9d\xe8\xa8\x80-\xe7\xba\xaf\xe7\x99\xbd\xe8\x8a\xb1\xe5\xab\x81-41-photos-35230',
                b'https://buondua.com/paranhosu-inah-\xec\x9d\xb4\xeb\x82\x98-vol-1-flowering-part-2-bonus-79-photos-35160'
            ]
        )

        self.assertEqual(
            self.album_parser.extract_pages(),
            [
                b'https://buondua.com/hot',
                b'https://buondua.com/hot?start=20',
                b'https://buondua.com/hot?start=40',
                b'https://buondua.com/hot?start=60',
                b'https://buondua.com/hot?start=80',
                b'https://buondua.com/hot?start=100',
                b'https://buondua.com/hot?start=120',
                b'https://buondua.com/hot?start=140',
                b'https://buondua.com/hot?start=160',
                b'https://buondua.com/hot?start=180',
            ]
        )

    def test_extract(self):
        data = self.album_parser.extract()
        albums = data.pop('albums')

        self.assertEqual(
            len(albums),
            200
        )

        self.assertEqual(
            albums[20:40],
            [
                b'https://buondua.com/pure-media-vol-270-hina-\xed\x9e\x88\xeb\x82\x98-sira-\xec\x8b\x9c\xeb\x9d\xbc-\xe2\x80\x93-x-rated-contract-248-photos-35178',
                b'https://buondua.com/pure-media-vol-269-yeha-\xec\x98\x88\xed\x95\x98-\xe2\x80\x93-visit-suspicious-hospital-201-photos-35108',
                b'https://buondua.com/djawa-photo-\xe2\x80\x93-hendoong-\xe2\x80\x9cswimming-lessons-14\xe2\x80\x9d-147-photos-35262',
                b'https://buondua.com/le-lehf-095a-b-c-g-su-165-photos-35214',
                b'https://buondua.com/pure-media-vol-271-min-ah-\xeb\xaf\xbc\xec\x95\x84-\xe2\x80\x93-kidnapping-119-photos-35236',
                b'https://buondua.com/coser-\xe6\xb0\xb4\xe6\xb7\xbcaqua-\xe7\xa2\xa7\xe8\x93\x9d\xe6\xa1\xa3\xe6\xa1\x88-\xe2\x80\x93-\xe4\xb8\x80\xe4\xb9\x8b\xe6\xbf\x91\xe6\x98\x8e\xe6\x97\xa5\xe5\xa5\x88\xe5\xa5\xb3\xe4\xbb\x86b-155-photos-35231',
                b'https://buondua.com/fantasy-story-white-sugar-vol-1-dress-check-for-a-poopy-skirt-113-photos-35285',
                b'https://buondua.com/bimilstory-hayun-\xed\x95\x98\xec\x9c\xa4-vol-01-debut-\xe2\x80\x93-flower-in-blossom-part-1-91-photos-35270',
                b'https://buondua.com/bimilstory-bomi-\xeb\xb3\xb4\xeb\xaf\xb8-vol-40-oil-shower-100-photos-35192',
                b'https://buondua.com/kang-in-kyung-\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos-35006',
                b'https://buondua.com/fantasy-story-banhee-vol-6-cafe-female-owner-98-photos-35218',
                b'https://buondua.com/glamarchive-baek-sul-tang-vol-19-football-fan-47-photos-35200',
                b'https://buondua.com/glamarchive-woo-u-\xec\x9a\xb0\xec\x9c\xa0-vol-13-diva-40-photos-35237',
                b'https://buondua.com/pure-media-vol-267-hina-\xed\x9e\x88\xeb\x82\x98-\xe2\x80\x93-black-out-slutty-girl-244-photos-34769',
                b'https://buondua.com/le-ledg-064a-jeong-ah-\xec\xa0\x95\xec\x95\x84-50-photos-35234',
                b'https://buondua.com/maruemon-\xeb\xa7\x88\xeb\xa3\xa8\xec\x97\x90\xeb\xaa\xbd-bunny-87-photos-35158',
                b'https://buondua.com/rina\xe3\x83\xa2\xe3\x83\xa2\xe3\x83\xaa\xe3\x83\x8a-momorina-momo-tako-127-photos-35161',
                b'https://buondua.com/bimilstory-nara-\xeb\x82\x98\xeb\x9d\xbc-vol-33-fantasy-harness-112-photos-35217',
                b'https://buondua.com/coser-\xe4\xb9\x9d\xe8\xa8\x80-\xe7\xba\xaf\xe7\x99\xbd\xe8\x8a\xb1\xe5\xab\x81-41-photos-35230',
                b'https://buondua.com/paranhosu-inah-\xec\x9d\xb4\xeb\x82\x98-vol-1-flowering-part-2-bonus-79-photos-35160'
            ]
        )
